package com.cndw.kungfu.business;

import java.util.List;

import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;

import com.cndw.kungfu.comm.GmCommand;
import com.cndw.kungfu.core.cache.CacheDisciple;
import com.cndw.kungfu.core.cache.CacheGoods;
import com.cndw.kungfu.core.cache.CacheLottery;
import com.cndw.kungfu.core.cache.CacheMonster;
import com.cndw.kungfu.core.cache.CacheNpc;
import com.cndw.kungfu.core.cache.CachePendant;
import com.cndw.kungfu.core.cache.CachePet;
import com.cndw.kungfu.core.cache.CacheQuiz;
import com.cndw.kungfu.core.cache.CacheScene;
import com.cndw.kungfu.core.cache.CacheSkill;
import com.cndw.kungfu.core.cache.CacheStory;
import com.cndw.kungfu.core.cache.CacheTask;
import com.cndw.kungfu.core.cache.CacheTitle;
import com.cndw.kungfu.core.cache.CacheUndercity;
import com.cndw.kungfu.core.cache.CacheUnion;
import com.cndw.kungfu.core.cache.CacheUpgrade;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.LangConfigLib;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.activity.domain.CarnivalConfig;
import com.cndw.kungfu.model.activity.domain.CrossServer;
import com.cndw.kungfu.model.activity.domain.KeyChest;
import com.cndw.kungfu.model.activity.domain.RateChest;
import com.cndw.kungfu.model.disciple.PlayerMartial;
import com.cndw.kungfu.model.disciple.domain.Disciple;
import com.cndw.kungfu.model.general.RankingListManager;
import com.cndw.kungfu.model.goods.domain.HeroShop;
import com.cndw.kungfu.model.goods.domain.SysModeAwards;
import com.cndw.kungfu.model.goods.domain.WarShop;
import com.cndw.kungfu.model.operation.domain.TreeFruit;
import com.cndw.kungfu.model.player.domain.Office;
import com.cndw.kungfu.model.pvp.api.CommApiClient;
import com.cndw.kungfu.model.siege.domain.SiegeAwards;
import com.cndw.kungfu.model.task.PrestigeGoods;
import com.cndw.kungfu.vo.rankingvo.OtherRankingVO;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.annotation.UnAuth;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

@Controller
public class BusinessCache {
	@Autowired
	private RankingListManager rankingListManager;

	private static Log loger = LogUtil.getMain();

	@Autowired
	private CacheDisciple cacheDisciple;
	@Autowired
	private CacheGoods cacheGoods;
	@Autowired
	private CacheMonster cacheMonster;
	@Autowired
	private CacheNpc cacheNpc;
	@Autowired
	private CachePendant cachePendant;
	@Autowired
	private CacheQuiz cacheQuiz;
	@Autowired
	private CacheScene cacheScene;
	@Autowired
	private CacheSkill cacheSkill;
	@Autowired
	private CacheStory cacheStory;
	@Autowired
	private CacheTask cacheTask;
	@Autowired
	private CacheUndercity cacheUndercity;
	@Autowired
	private CacheUnion cacheUnion;
	@Autowired
	private CacheUpgrade cacheUpgrade;
	@Autowired
	private CacheTitle cacheTitle;
	@Autowired
	private CachePet cachePet;
	@Autowired
	private CacheLottery cacheLottery;

	@GmCommand
	@UnAuth
	public ResponseMessage reload(Session session, String sign, int type) {

		if ((type & 1 << 1) > 0) {
			loger.info("clear cacheDisciple");
			cacheDisciple.remove();
		}
		if ((type & 1 << 2) > 0) {
			loger.info("clear cacheGoods");
			cacheGoods.remove();
			cacheMonster.remove();
			cachePendant.remove();

		}
		if ((type & 1 << 3) > 0) {
			loger.info("clear cacheMonster");
			cacheMonster.remove();
		}
		if ((type & 1 << 4) > 0) {
			loger.info("clear cacheNpc");
			cacheNpc.remove();
		}
		if ((type & 1 << 5) > 0) {
			loger.info("clear cachePendant");
			cachePendant.remove();
		}
		if ((type & 1 << 6) > 0) {
			loger.info("clear cacheQuiz");
			cacheQuiz.remove();
		}
		if ((type & 1 << 7) > 0) {
			loger.info("clear cacheScene");
			cacheScene.remove();
		}
		if ((type & 1 << 8) > 0) {
			loger.info("clear cacheSkill");
			cacheSkill.remove();
		}
		if ((type & 1 << 9) > 0) {
			loger.info("clear cacheStory");
			cacheStory.remove();
		}
		if ((type & 1 << 10) > 0) {
			loger.info("clear cacheTask");
			cacheTask.remove();
		}
		if ((type & 1 << 11) > 0) {
			loger.info("clear cacheUndercity");
			cacheUndercity.remove();
		}
		if ((type & 1 << 12) > 0) {
			loger.info("clear cacheUnion");
			cacheUnion.remove();
		}
		if ((type & 1 << 13) > 0) {
			loger.info("clear cacheUpgrade");
			cacheUpgrade.remove();
		}

		if ((type & 1 << 14) > 0) {
			loger.info("clear cacheTitle");
			cacheTitle.remove();
		}
		if ((type & 1 << 15) > 0) {
			loger.info("clear cacheUpgrade");
			cachePet.remove();
		}
		if ((type & 1 << 16) > 0) {
			loger.info("clear cacheLottery");
			cacheLottery.remove();
		}

		if ((type & 1 << 17) > 0) {
			loger.info("clear CarnivalConfig");
			CarnivalConfig.clear();
		}

		return ResponseMessage.getSuccMessage(true);
	}

	@GmCommand
	@UnAuth
	public ResponseMessage clacPower(Session session, String sign) {
		rankingListManager.clearUpdateTime(2);
		List<OtherRankingVO> a = rankingListManager.getDiscipleRanking();
		for (OtherRankingVO e : a) {
			Player target = Context.getPlayerServer().getByPlayerId(e.getPlayerId());
			if (target != null) {
				PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(target.getPlayerId());
				for (Disciple ee : playerMartial.getDisciples()) {
					int score = ee.getScore();
					ee.clacScore();
					if (score != ee.getScore()) {
						ee.commitSync();
					}
				}
			}
		}

		return ResponseMessage.getSuccMessage(true);
	}

	@GmCommand
	@UnAuth
	public ResponseMessage cacheFile(Session session, String sign, int type) {
		switch (type) {
		case 1:
			HeroShop.clear();
			break;
		case 2:
			WarShop.clear();
			break;
		case 3:
			CrossServer.clear();
			CommApiClient.reloadServers();
			break;
		case 4:
			CarnivalConfig.clear();
			break;
		case 5:
			KeyChest.reload();
			break;
		case 6:
			Office.clear();
			break;
		case 7:
			RateChest.load();
			break;
		case 8:
			SiegeAwards.updateConfig();
			break;
		case 9:
			PrestigeGoods.reload();
			break;
		case 10:
			SysModeAwards.reload();
			break;
		case 11:
			TreeFruit.clear();
			break;
		case 13:
			LangConfigLib.clear();
			break;
		case 14:
			LanguageLib.clear();
			break;
		default:
			break;
		}

		LogUtil.getMain().debug("cacheFile " + type);
		return ResponseMessage.getSuccMessage(true);
	}

	
	@UnAuth
	@GmCommand
	public ResponseMessage taskDebug(Session session,String sign,int type){
		
		return ResponseMessage.getSuccMessage(true);
	}
}
